package com.authorization.project.authorization.controller;

import java.util.List;

import com.authorization.common.utils.ServletUtils;
import com.authorization.framework.security.LoginUser;
import com.authorization.project.authorization.service.IPackageMoudleMappingService;
import com.authorization.project.system.domain.SysMenu;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.authorization.framework.aspectj.lang.annotation.Log;
import com.authorization.framework.aspectj.lang.enums.BusinessType;
import com.authorization.project.authorization.domain.Module;
import com.authorization.project.authorization.service.IModuleService;
import com.authorization.framework.web.controller.BaseController;
import com.authorization.framework.web.domain.AjaxResult;
import com.authorization.common.utils.poi.ExcelUtil;
import com.authorization.framework.web.page.TableDataInfo;

/**
 * 模块Controller
 * 
 * @author 李晓雷
 * @date 2020-04-22
 */
@RestController
@RequestMapping("/authorization/module")
public class ModuleController extends BaseController
{
    @Autowired
    private IModuleService moduleService;

    @Autowired
    private IPackageMoudleMappingService iPackageMoudleMappingService;

    /**
     * 查询模块列表
     */
    @PreAuthorize("@ss.hasPermi('authorization:module:list')")
    @GetMapping("/list")
    public TableDataInfo list(Module module)
    {
        startPage();
        List<Module> list = moduleService.selectModuleList(module);
        return getDataTable(list);
    }

    /**
     * 查询产品列表集合（参数）
     */
    @GetMapping(value = "/parentList/{productId}")
    public TableDataInfo gatParentList(@PathVariable Long productId)
    {
        List<Module> list = moduleService.parentList(productId);
        return getDataTable(list);
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping(value = "/treeselect/{productId}")
    public AjaxResult treeselect(@PathVariable Long productId)
    {
        AjaxResult ajax = AjaxResult.success();

        Module module = new Module();
        module.setProductId(productId);
        List<Module> modules = moduleService.selectModuleList(module);
        ajax.put("menus", moduleService.buildModulesTreeSelect(modules));
        ajax.put("baseMoudle", moduleService.selectBaseModuleIdsByProductId(productId));
        return ajax;
    }

    /**
     * 获取菜单下拉树列表通过模块和产品ID
     */
    @PostMapping(value = "/moduleProductTreeselect")
    public AjaxResult moduleProductTreeselect(@RequestBody  Module module)
    {
        List<Module> modules = moduleService.selectModuleList(module);

        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", iPackageMoudleMappingService.selectModuleIdsByPackageId(module.getPackageId()));
        ajax.put("menus", moduleService.buildModulesTreeSelect(modules));
        ajax.put("baseMoudle", moduleService.selectBaseModuleIdsByProductId(module.getProductId()));
        return ajax;
    }

    /**
     * 导出模块列表
     */
    @PreAuthorize("@ss.hasPermi('authorization:module:export')")
    @Log(title = "模块", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Module module)
    {
        List<Module> list = moduleService.selectModuleList(module);
        ExcelUtil<Module> util = new ExcelUtil<Module>(Module.class);
        return util.exportExcel(list, "module");
    }

    /**
     * 获取模块详细信息
     */
    @PreAuthorize("@ss.hasPermi('authorization:module:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(moduleService.selectModuleById(id));
    }

    /**
     * 新增模块
     */
    @PreAuthorize("@ss.hasPermi('authorization:module:add')")
    @Log(title = "模块", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Module module)
    {
        return toAjax(moduleService.insertModule(module));
    }

    /**
     * 修改模块
     */
    @PreAuthorize("@ss.hasPermi('authorization:module:edit')")
    @Log(title = "模块", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Module module)
    {
        return toAjax(moduleService.updateModule(module));
    }

    /**
     * 删除模块
     */
    @PreAuthorize("@ss.hasPermi('authorization:module:remove')")
    @Log(title = "模块", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(moduleService.deleteModuleByIds(ids));
    }
}
